package yxy.game.pm2.bean;

import org.redisson.api.RAtomicLong;

import yxy.apple.util.redisson.RedisConnection;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.cherry.game.base.redis.RedisType;

/** 零碎的变量 */
public class Variable {

	static private RedisConnection redis() {
		return RedisManager.getInstance().getConnection(RedisType.REDIS_GAME);
	}

	static private RAtomicLong touchVariable(String playerCode, String variableName) throws RedisException {
		String key = String.format("PLAYER:%s:VAR:%s", playerCode, variableName);
		return RedisManager.client(redis()).getAtomicLong(key);
	}

	static public Long get(String playerCode, String variableName) throws RedisException {
		RAtomicLong bucket = touchVariable(playerCode, variableName);
		Long value = bucket.get();
		return value;
	}

	static public Long add(String playerCode, String variableName, int add) throws RedisException {
		RAtomicLong bucket = touchVariable(playerCode, variableName);
		Long value = bucket.addAndGet(add);
		return value;
	}

}
